using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Collections.Generic;

/// <summary>
/// Summary description for CrmHelper
/// </summary>
public class CrmHelper
{
    private string _server = string.Empty;
    private string _orgName = string.Empty;
    private CrmSdk.CrmService _crmService = null;
    private MetadataSdk.MetadataService _metadataService = null;
    private IDictionary<string, MetadataSdk.EntityMetadata> _metadataCache = null;

	public CrmHelper()
	{
		CreateServices();
	}

	public CrmHelper(string orgName)
	{
		_orgName = orgName;
		CreateServices();
	}
	
	public CrmHelper(string server, string orgName)
	{
        _server = server;
        _orgName = orgName;
        CreateServices();
	}

    public string OrgName
    {
        get { return _orgName; }
    }

    public string Server
    {
        get { return _server; }
    }

    private void CreateServices()
    {
		string server = this._server;
		string orgName = this.OrgName;
		string crmServiceUrl = string.Empty;
		string metadataServiceUrl = string.Empty;
		
		if(string.IsNullOrEmpty(server))
		{
			crmServiceUrl = ConfigurationManager.AppSettings["CrmSdk.CrmServiceWsdl"];
			metadataServiceUrl = ConfigurationManager.AppSettings["MetadataSdk.MetadataService"];			
		}
		else
		{
			//TODO
		}
		
		if(string.IsNullOrEmpty(orgName))
		{
			_orgName = orgName = ConfigurationManager.AppSettings["OrgName"];
		}
    
        CrmSdk.CrmAuthenticationToken crmAuthToken = new CrmSdk.CrmAuthenticationToken();
        crmAuthToken.OrganizationName = orgName;

        CrmSdk.CrmService crmService = new CrmSdk.CrmService();
        crmService.Credentials = System.Net.CredentialCache.DefaultCredentials;
        crmService.CrmAuthenticationTokenValue = crmAuthToken;
        crmService.Url = crmServiceUrl;

        _crmService = crmService;

        MetadataSdk.CrmAuthenticationToken metaAuthToken = new MetadataSdk.CrmAuthenticationToken();
        metaAuthToken.OrganizationName = orgName;

        MetadataSdk.MetadataService metadataService = new MetadataSdk.MetadataService();
        metadataService.Credentials = System.Net.CredentialCache.DefaultCredentials;
        metadataService.CrmAuthenticationTokenValue = metaAuthToken;
        metadataService.Url = metadataServiceUrl;
        _metadataService = metadataService;
    }

    public CrmSdk.CrmService GetCrmService()
    {
        if (_crmService == null)
        {
            CreateServices();
        }
        return _crmService;
    }

    public MetadataSdk.MetadataService GetMetadataService()
    {
        if (_metadataService == null)
        {
            CreateServices();
        }
        return _metadataService;
    }

    public MetadataSdk.EntityMetadata GetEntityMetadata(string entityName)
    {
        if (_metadataCache == null)
        {
            _metadataCache = new Dictionary<string, MetadataSdk.EntityMetadata>();
        }        
        MetadataSdk.EntityMetadata entityMetadata;
        if (_metadataCache.TryGetValue(entityName, out entityMetadata) == false)
        {
            MetadataSdk.MetadataService metaDataService = GetMetadataService();
            MetadataSdk.RetrieveEntityRequest request = new MetadataSdk.RetrieveEntityRequest();
            request.LogicalName = entityName;
            request.EntityItems = MetadataSdk.EntityItems.All;
            MetadataSdk.RetrieveEntityResponse response = metaDataService.Execute(request) as MetadataSdk.RetrieveEntityResponse;
            entityMetadata = response.EntityMetadata;
            _metadataCache[entityName] = entityMetadata;
        }
        return entityMetadata;
    }
}
